﻿/**
* 
* Copyright (c) 2012 Anders Høst Kjærgaard | ahkj@itu.dk
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, 
* including without limitation the rights to use, copy, modify, merge, publish, distribute, 
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using Flatland.AgentBehavior;
using Flatland.GeneticProgramming.SyntaxTree;

namespace Flatland.GeneticProgramming.CoreTypes {
    /// <summary>
    /// Genotype for genetic code.
    /// 
    /// Convention that implementers of the GenoType must follow.
    /// 1.	The inheriting class must be:
    ///     a.	public
    ///     b.	serializable
    ///     c.	cloneable
    ///     d.	exposing a public parameterless constructor.
    ///     e.	defining primitives for both the function and the primitive set.
    ///     f.	inheriting directly from the GenoType class.
    /// 2.	The primitives must be public.
    /// </summary>
    [Serializable]
    public abstract class GenoType : Behavior, ICloneable {
        /// <summary>
        /// Implementers should return a fitness value indicating the fitness of the generated code. 
        /// This value will normally make sense only in the context of a gentic run with a set fitness goal.
        /// </summary>
        /// <returns>A fitness value.</returns>
        public abstract double Fitness();

        /// <summary>
        /// Expression Tree representation of the genetic code
        /// </summary>
        public Tree Tree { get; set; }

        /// <summary>
        /// Reports must be clonable.
        /// </summary>
        /// <returns></returns>
        public virtual object Clone() {
            throw new NotImplementedException("Clone must be implemented");
        }

        /// <summary>
        /// Execute the generated code.
        /// </summary>
        /// <returns></returns>
        public object Execute(){
            return Tree.Execute(this);
        }
    }
}
